package test;

import bean.Emp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {

    private static SqlSessionFactory ssf;

    static {
        try {
            // 1⃣️ 加载配置文件
            InputStream in = Resources.getResourceAsStream("config.xml");
            // 2⃣️ 创建一个会话工厂
            ssf = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void a() throws IOException {
        // 1⃣️ 加载配置文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 2⃣️ 创建一个会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 3⃣️ 建立Java同MySQL之间的会话
        SqlSession session = ssf.openSession();
        // 4⃣️ 获取相应接口的代理对象
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        // 5⃣️ 调用方法
        List<Emp> list = mapper.findAllEmp();
        for (Emp emp : list) {
            System.out.println(emp);
        }
        // 6⃣️ 关闭会话
        session.close();
    }

    public static void b() throws IOException {
        Emp emp = new Emp("郭杨柳", "程序员", 7788, 3200.0, 100.0, 10);
        // 3⃣️ 建立Java同MySQL之间的会话
        SqlSession session = ssf.openSession();
        // 4⃣️ 获取相应接口的代理对象
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        // 5⃣️ 调用方法
        mapper.saveEmp(emp);
        // 6⃣️ 提交事务(增删改)
        session.commit();
        // 7⃣️ 关闭会话
        session.close();
    }

    public static void c() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.deleteEmp(7936);
        session.commit();
        session.close();
    }

    public static void d() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.updateEmp(7954, "杨智宇");
        session.commit();
        session.close();
    }

    public static void d1() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEname("杨智宇");
        emp.setEmpNo(7954);
        empMapper.updateEmp1(emp);
        session.commit();
        session.close();
    }

    public static void d2() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("id", 7970);
        map.put("ename", "郑甜甜");
        empMapper.updateEmp2(map);
        session.commit();
        session.close();
    }

    public static void d3() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.updateEmp3(7991, "景迎彬");
        session.commit();
        session.close();
    }

    public static void e() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        Emp emp = empMapper.findEmpByNo(7369);
        System.out.println(emp);
        session.close();
    }

    public static void f() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.findEmpByEname("'%张%'");
        for (Emp emp : list
        ) {
            System.out.println(emp);
        }
        session.close();
    }

    public static void g1() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.findEmpByPage1(5, 5);
        for (Emp emp : list
        ) {
            System.out.println(emp);
        }
        session.close();
    }

    public static void g2() throws IOException {
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        PageHelper.startPage(2,5);
        List<Emp> list = mapper.findAllEmp();
        for (Emp emp : list) {
            System.out.println(emp);
        }
        PageInfo<Emp> pi = new PageInfo<>(list);
        System.out.println(pi);
        session.close();

    }

    public static void main(String[] args) throws IOException {
        g2();
    }
}